package org.jsola.hr.constant;

import org.jsola.core.validator.IConst;
import org.jsola.json.JsonKit;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wangxuezheng
 */
public enum PunchType implements IConst {

    /**
     * 上班打卡
     */
    ON_WORK(1,"上班打卡"),
    /**
     * 下班打卡
     */
    OFF_WORK(2,"下班打卡"),
    /**
     * 上班缺卡
     */
    COMPLETE(3,"上班缺卡"),
    /**
     * 下班缺卡
     */
    INVALID_PUNCH(4,"下班缺卡");

    private final Integer value;
    private final String desc;
    /**
     * 枚举值罗列，给swagger接口文档展示用
     */
    public static final String VALUES_STR = "1,2,3,4";

     /**
     * 缓存所有value
     */
    private static final Map<Integer, PunchType> LOOKUP = new HashMap<>();

    static {
        for (PunchType e : PunchType.values()) {
            LOOKUP.put(e.value, e);
        }
    }


    PunchType(Integer value, String desc) {
        this.value = value;
        this.desc = desc;
    }

    /**
     * 根据value查找PunchType
     * @param value value
     * @return PunchType
     */
    public static PunchType find(Integer value) {
        return LOOKUP.get(value);
    }

    /**
     * 根据desc查找PunchType
     * @param desc desc
     * @return PunchType
     */
    public static PunchType findByDesc(String desc){
        for (PunchType e : PunchType.values()) {
            if(e.getDesc().equals(desc)){
                return e;
            }
        }
        return null;
    }

    /**
     * 所有枚举转json
     * @return json
     */
    public static String getAllToJson() {
        Map<String,Object> map = new HashMap<>();
        for (PunchType e : PunchType.values()) {
            map.put("value", e.value);
            map.put("desc", e.desc);
        }
        return JsonKit.toJSONString(map);
    }

    @Override
    public boolean check(Object obj) {
        Integer value = (Integer) obj;
        return find(value) != null;
    }

    public Integer getValue() {
        return value;
    }

    public String getDesc() {
        return desc;
    }
}
